#robweixin tv_adjust.py
#coding=utf-8
#encoding=utf-8
import sys
import udp_command as ucmd
reload(sys)
sys.setdefaultencoding('utf8')

#sys.path.append('/home/lxp/myprojects/pysql') #在环境变量中增加搜索路径

#加载通用模块
import json
import requests
from itchat.content import *
import itchat
import opcv_pkg as cvpkg

KEY = '858e96072456465e8279296e85449be2' #图灵key

def get_response(msg):


    apiUrl = 'http://www.tuling123.com/openapi/api'
    data = {
        'key'    : KEY,
        'info'   : msg,
        'userid' : 'wechat-robot',
    }
    try:
        r = requests.post(apiUrl, data=data).json()
        print(r)
	if r.get('url') == None:
		reply = r.get('text')
	else:
		reply = r.get('text') + u'链接:' + r.get('url')

        return reply

    except:
        return reply

@itchat.msg_register([PICTURE, ATTACHMENT, VIDEO], isGroupChat = True) #群聊对图片、附件、视频的答复
def Pic_Att_Vid_reply_groupchat(msg):
    msg['Text'](msg['FileName'])
    f=msg['FileName']
    print(f)
    cvpkg.count_histogram(msg['FileName'])
    screen,adjust=cvpkg.tv_contours(msg['FileName'])
#    itchat.send_image('./screen_histogram.png',toUserName=msg['FromUserName']) #直方图
#    itchat.send_image('./screen_contours.jpg', toUserName=msg['FromUserName'])
    reply = '测量' + json.dumps(screen) +' 调节'+ json.dumps(adjust)
#    ucmd.fill_table(adjust)
    return reply

@itchat.msg_register(TEXT) #私聊对文本的答复
def text_reply(msg):

    umsg = msg['Text']
    if umsg[0] == ':':
      cmdstr = umsg[1:]
      if cmdstr == u'屏幕编号':
         itchat.send_image('./screen_contours.jpg', toUserName=msg['FromUserName'])
      if cmdstr == u'直方图':
         itchat.send_image('./screen_histogram.png',toUserName=msg['FromUserName']) #直方图


    else:

      print(umsg)
      defaultReply = 'I received: ' + msg['Text']


      reply = get_response(umsg)
      #reply['Url']='http://m.xiachufang.com/recipe/100113803/?ref=tuling'
      print(msg['FromUserName'] + u':' + reply)
      return reply

@itchat.msg_register([PICTURE, ATTACHMENT, VIDEO]) #私聊对图片、附件、视频的答复
def Pic_Att_Vid_reply(msg):
    msg['Text'](msg['FileName'])
    f=msg['FileName']
    print(f)
    #:bgr_f = cvpkg.BGR_feature(msg['FileName'])
    cvpkg.count_histogram(msg['FileName'])
    screen,adjust=cvpkg.tv_contours(msg['FileName'])
    itchat.send_image('./screen_histogram.png',toUserName=msg['FromUserName'])
    #itchat.send_image('./screen_edged.jpg', toUserName=msg['FromUserName'])
    itchat.send_image('./screen_contours.jpg', toUserName=msg['FromUserName'])
    #reply = 'Rmax:' + str(bgr_f['Rmax'][0][0]) + str(bgr_f['Rmax'][1])
    #reply = reply + ', Gmax:' + str(bgr_f['Gmax'][0][0]) + str(bgr_f['Gmax'][1])
    #reply = reply + ', Bmax:' + str(bgr_f['Bmax'][0][0]) + str(bgr_f['Bmax'][1])
    reply = '测量' + json.dumps(screen) +' 调节'+ json.dumps(adjust)
    return reply


@itchat.msg_register(FRIENDS)
def add_friend(msg):
    itchat.add_friend(**msg['Text']) # 该操作会自动将新好友的消息录入，不需要重载通讯录，微信不要开启“加好友无需验证”
    itchat.send_msg('很高兴认识你！Nace to meet you!', msg['RecommendInfo']['UserName'])

itchat.auto_login(enableCmdQR=0, hotReload=True)
itchat.run()
